﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using System;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;

namespace SBT
{
    public class VerticalPageFlipScroller : PageFlipScrollerBase
    {

        protected override string TranslateAxis
        {
            get { return "Y"; }
        }
        private Button m_topButton;
        public Button TopButton
        {
            get
            {
                return m_topButton;
            }
            set
            {
                if (m_topButton != value)
                {
                    m_topButton = value;
                    m_topButton.add_click(m_onTopClick);
                }
            }
        }
        private Button m_bottomButton;
        public Button BottomButton
        {
            get
            {
                return m_bottomButton;
            }
            set
            {
                if (m_bottomButton != value)
                {
                    m_bottomButton = value;
                    m_bottomButton.add_click(m_onBottomClick);
                }
            }
        }

        seh m_onTopClick;
        seh m_onBottomClick;

        private void OnTopClick(object sender, sea args)
        {
            if ((bool)(object)ScrollContainer)
            {
                double current = AnimRunning ? -DoubleKey.Value : ScrollContainer.ScrollY;
                double step = ((bool)(object)StepSize)?StepSize : ScrollContainer.Height;
                double realMax = ScrollContainer.MaxScrollY;
                double maxPage = Math.Ceil(realMax / step);
                realMax = maxPage * step;
                double currentPage = Math.Ceil(current / step);
                double to = 0;
                if (currentPage <= 0)
                    to = 0;
                else if (currentPage >= maxPage)
                    to = realMax - step;
                else
                {
                    to = step * (currentPage - 1);
                }
                if (to == current)
                    return;
                //m_scrollContainer.ScrollX = to;
                InitAnim();
                
                DoubleKey.Value = -to;
                BeginAnimation();
            }
        }
        private void OnBottomClick(object sender, sea args)
        {
            if ((bool)(object)ScrollContainer)
            {
                double current = AnimRunning ? -DoubleKey.Value : ScrollContainer.ScrollY;
                double step = ((bool)(object)StepSize) ? StepSize : ScrollContainer.Height;
                double realMax = ScrollContainer.MaxScrollY;
                double maxPage = Math.Ceil(realMax / step);
                realMax = maxPage * step;
                double currentPage = Math.Ceil(current / step);
                double to = 0;
                if (currentPage <= 0)
                    to = step;
                else if (currentPage >= maxPage)
                    to = realMax;
                else
                {
                    to = step * (currentPage + 1);
                }
                if (to == current)
                    return;
                //m_scrollContainer.ScrollX = to;
                InitAnim();
                
                DoubleKey.Value = -to;
                BeginAnimation();
            }
        }


        public VerticalPageFlipScroller()
        {

            m_onTopClick = OnTopClick;
            m_onBottomClick = OnBottomClick;
        }

    }
}